import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { defineStore } from 'pinia'
import { clearAwardRecords, getActivity } from '@/api'
import { useFullscreen } from '@/hooks/use-fullscreen'
import { useQrcode } from '@/hooks/use-qrcode'
import { useBackgroundAudio } from '@/hooks/use-background-audio'
import { Prize } from '@/models/Prize'
import { PrizeLevel } from '@/enums/prize'
import { LotteryDrawingStatus, LotteryType } from '@/enums/lottery'
import { Lottery } from '@/models/Lottery'
import type { Winner } from '@/models/Winner'
import type { TGetActivityData } from '@/api/index'

export const useAppStore = defineStore('app', () => {
  const router = useRouter()

  const activity = ref<TGetActivityData>()
  const prizes = ref<[]>([])

  onMounted(async () => {
    // const { data } = await getActivity()
    // activity.value = data.data
  })

  const { isShowQrcode, toggleQrcodeVisibility } = useQrcode()
  const { isFullscreen, toggleFullscreen } = useFullscreen()
  const { isPaused, switchAudio, toggleAudio, play, pause } = useBackgroundAudio()

  const lotteryMap = new Map<LotteryType, Lottery>([
    [
      LotteryType.NAME_LIST_LOTTERY,
      new Lottery(
        LotteryType.NAME_LIST_LOTTERY,
        new Map([
          [PrizeLevel.FIRST_PRIZE, new Prize(1, '电脑', PrizeLevel.FIRST_PRIZE, 50, '')],
          [PrizeLevel.SECOND_PRIZE, new Prize(2, '手机', PrizeLevel.SECOND_PRIZE, 50, '')],
        ]),
      ),
    ],
    [
      LotteryType.SCAN_CODE_LOTTERY,
      new Lottery(
        LotteryType.SCAN_CODE_LOTTERY,
        new Map([
          [PrizeLevel.FIRST_PRIZE, new Prize(1, '电脑', PrizeLevel.FIRST_PRIZE, 50, '')],
          [PrizeLevel.SECOND_PRIZE, new Prize(2, '手机', PrizeLevel.SECOND_PRIZE, 50, '')],
        ]),
      ),
    ],
  ])

  const currentLottery = ref(lotteryMap.get(LotteryType.SCAN_CODE_LOTTERY) as Lottery)

  function increment() {
    const numberOfDraws = currentLottery.value.getNumberOfDraws()
    currentLottery.value.setNumberOfDraws(numberOfDraws + 1)
  }

  function decrement() {
    const numberOfDraws = currentLottery.value.getNumberOfDraws()
    if (numberOfDraws !== 0) {
      currentLottery.value.setNumberOfDraws(numberOfDraws - 1)
    }
  }

  const prizeLevels = [PrizeLevel.FIRST_PRIZE, PrizeLevel.SECOND_PRIZE]
  const currentPrizeLevelIndex = ref(0)

  function setPreviousPrizeLevelToCurrent() {
    if (currentPrizeLevelIndex.value === 0) {
      currentPrizeLevelIndex.value = prizeLevels.length - 1
    } else {
      currentPrizeLevelIndex.value -= 1
    }
    const prizeLevel = prizeLevels[currentPrizeLevelIndex.value]
    currentLottery.value.setCurrentPrizeLevel(prizeLevel)
  }

  function setNextPrizeLevelToCurrent() {
    if (currentPrizeLevelIndex.value === prizeLevels.length - 1) {
      currentPrizeLevelIndex.value = 0
    } else {
      currentPrizeLevelIndex.value += 1
    }
    const prizeLevel = prizeLevels[currentPrizeLevelIndex.value]
    currentLottery.value.setCurrentPrizeLevel(prizeLevel)
  }

  function navigateToNameListLottery() {
    currentLottery.value = lotteryMap.get(LotteryType.NAME_LIST_LOTTERY) as Lottery
    router.push('/name-list-lottery')
  }

  function navigateToScanCodeLottery() {
    currentLottery.value = lotteryMap.get(LotteryType.SCAN_CODE_LOTTERY) as Lottery
    router.push('/scan-code-lottery')
  }

  const drawingStatus = ref(LotteryDrawingStatus.NOT_STARTED)

  function draw() {
    drawingStatus.value = LotteryDrawingStatus.IN_PROGRESS
    setTimeout(() => {
      drawingStatus.value = LotteryDrawingStatus.COMPLETED
      setTimeout(() => {
        setLastWinners()
        lastWinnersVisible.value = true
      }, 500)
    }, 1500)
  }

  function resetDrawingStatus() {
    drawingStatus.value = LotteryDrawingStatus.NOT_STARTED
    switchAudio('LOTTERY');
  }

  const lastWinnersVisible = ref(false)
  const lastWinners = ref<Winner[]>([])

  function closeLastWinners() {
    lastWinnersVisible.value = false
    lastWinners.value = []
    drawingStatus.value = LotteryDrawingStatus.NOT_STARTED
  }

  async function setLastWinners() {
    // lastWinners.value = await fetchWinners()
  }

  const clearVisible = ref(false)

  function openClear() {
    clearVisible.value = true
  }

  function closeClear() {
    clearVisible.value = false
  }

  async function clear() {
    clearVisible.value = false
    await clearAwardRecords()
  }

  return {
    activity,
    clear,

    currentLottery,
    setPreviousPrizeLevelToCurrent,
    setNextPrizeLevelToCurrent,
    increment,
    decrement,

    navigateToNameListLottery,
    navigateToScanCodeLottery,

    drawingStatus,
    draw,
    resetDrawingStatus,

    lastWinnersVisible,
    lastWinners,
    closeLastWinners,

    clearVisible,
    openClear,
    closeClear,

    isShowQrcode,
    toggleQrcodeVisibility,

    isFullscreen,
    toggleFullscreen,

    isPaused,
    switchAudio,
    toggleAudio,
    play,
    pause,
  }
})
